package geektime.concurrent.race;

import java.util.Arrays;

public class SimpleDivideCompute2 implements ComputeRunnable {

    SimpleShareData ssd;
    int size;
    int offset;

    public SimpleDivideCompute2(SimpleShareData ssd, int size, int offset) {
        this.ssd = ssd;
        this.size = size;
        this.offset = offset;
    }

    @Override
    public void run() {
        go();
    }

    @Override
    public void go() {
        System.out.println("开始计算随机数: " + size + " " + offset);
        Integer[] alist = new Integer[size];
        Integer[] scoreList = ssd.getScore().toArray(new Integer[0]);
        System.arraycopy(scoreList, offset * size, alist, 0, size);

        Arrays.sort(alist);
        for (int i = 0; i < SimpleShareData.BUFSIZE * size / SimpleShareData.COUNT; i++) {
            // System.out.println("随机数: " + alist[alist.length - i - 1]);
            ssd.addExchange(alist[alist.length - i - 1]);
        }
        ssd.getCompSig().countDown();
        // System.out.println("计算随机数完毕: " + size + " " + SimpleShareData.BUFSIZE * size
        // / SimpleShareData.COUNT);
    }
}
